package com.fhz.circle.index.ui.mine;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.fhz.circle.R;
import com.fhz.circle.databinding.FragmentNotificationsBinding;
import com.fhz.circle.databinding.MineFragment1Binding;
import com.fhz.circle.databinding.MineFragment1Binding;
import com.fhz.circle.index.ui.notifications.NotificationsViewModel;
import com.fhz.circle.manager.DialogManager;
import com.fhz.circle.widget.DialogView;
import com.fhz.core_lib.base.BaseApplication;
import com.fhz.module_mine.fileutils.FileHelper;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import org.json.JSONObject;

import java.io.File;

import io.reactivex.functions.Consumer;

public class MineFragment extends Fragment implements View.OnClickListener {

    private MineViewModel mViewModel;

    public MineFragment1Binding binding;

    public DialogView selectProfileDialog;

    private RxPermissions permissions ;

    private File uploadFile = null;
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        permissions= new RxPermissions(this);
        //权限申请
        permissions.request(Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_CONTACTS,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.CALL_PHONE
                ).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(!aBoolean){
                    Toast.makeText(getActivity(), "必须都要同意哦", Toast.LENGTH_SHORT).show();
                }
            }
        });
        mViewModel = new ViewModelProvider(this,new MineViewModelFactory()).get(MineViewModel.class);
        binding = MineFragment1Binding.inflate(inflater, container, false);
        View root = binding.getRoot();


        binding.profileImage.setOnClickListener(this);
        selectProfileDialog = DialogManager.getInstance().createDialog(getActivity(),R.layout.select_dialog_layout, Gravity.BOTTOM);
        selectProfileDialog.findViewById(R.id.take_picture).setOnClickListener(this);
        selectProfileDialog.findViewById(R.id.open_album).setOnClickListener(this);
        selectProfileDialog.findViewById(R.id.cancel).setOnClickListener(this);


        return root;

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.profile_image:
                DialogManager.getInstance().show(selectProfileDialog);
                break;
            case R.id.take_picture:
                DialogManager.getInstance().hide(selectProfileDialog);
                //打开相机--> 需要权限
                ///storage/emulated/0/feng.jpg
//                File file = new File("/storage/emulated/0/feng.jpg");

                //跳转到相机
                FileHelper.getInstance().toCamera(getActivity());
                break;

            case R.id.open_album:
                //访问本地的相册 --> 需要权限
                DialogManager.getInstance().hide(selectProfileDialog);
                //跳转到相册
                FileHelper.getInstance().toAlbum(getActivity());
                break;
            case R.id.cancel:
                DialogManager.getInstance().hide(selectProfileDialog);

                break;


        }
    }

    //不管是相机还是本地的图片,都是要最后上传的.

    /**
     * 上传逻辑是这样的
     * 先从后台取到七牛的token
     * 然后在前端上传图片到七牛
     * 上传成功后,把图片的完整路径发给后端
     * 后端存到数据库
     * 这样用户的信息每次读取都是真是的图片地址
     * @param requestCode
     * @param resultCode
     * @param data
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i("requestCode:" , requestCode+"");
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == FileHelper.CAMEAR_REQUEST_CODE) {
                FileHelper.getInstance().startPhotoZoom(getActivity(), FileHelper.getInstance().getTempFile());
            } else if (requestCode == FileHelper.ALBUM_REQUEST_CODE) {
                Uri uri = data.getData();
                if (uri != null) {
                    String path = FileHelper.getInstance().getRealPathFromURI(BaseApplication.getInstance().getApplicationContext(), uri);
                    if (!TextUtils.isEmpty(path)) {
                        uploadFile = new File(path);
                        FileHelper.getInstance().startPhotoZoom(getActivity(), uploadFile);
                    }
                }
            } else if (requestCode == FileHelper.CAMERA_CROP_RESULT) {
                Log.i("a","CAMERA_CROP_RESULT");
                uploadFile = new File(FileHelper.getInstance().getCropPath());
                Log.i("uploadPhotoFile:" , uploadFile.getPath());
            }
            //设置头像
            if (uploadFile != null) {
                Bitmap mBitmap = BitmapFactory.decodeFile(uploadFile.getPath());
                binding.profileImage.setImageBitmap(mBitmap);

                //头像上传
                UploadManager uploadManager = new UploadManager();
                //随机生成一个文件名称


            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }




    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


}